"""
A class to exercise the JT core functions.
"""
import time

import itk
import vtk

import connect2
import ImageCompare

import JT
import Calibration

import StandAlone_GetMetric2 as itkfx

def Test():
    """
    Test out the ITK registration procedure.
    """

    inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
    staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
    calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'

    fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Images/'
    baseName = 'DRR_'

    #
    #  ITK Image Components
    #%#####################
    itk_reader = itk.ImageFileReader.IF2.New()
    itk_reader.SetFileName(fixedImageName)
    itk_reader.Update()

    #
    # Create Lateral DRR
    #%###########################
    xraycamLat = JT.XRayCamera()
    drrLat = JT.DRR()
    calLat = Calibration.ExternalCalibration()

    calLat.LoadConsolidatedCalibration(calFileLat)
    calLat.LoadStaFile(staFile)
    drrLat.SetXRayCamera(xraycamLat)
    drrLat.SetVolumeFilename(inputVolFileName)
    drrLat.SetBlackOnWhiteImage()

    #
    # Automatic calibration
    #%##########################
    xraycamLat.SetExternalCalibration(calLat)

    vtkMatrixLAT = calLat.ConvertNumpyToVTKArray(calLat._VolumeT)
    drrLat.SetVolumeUserMatrix(vtkMatrixLAT)
    drrLat.GenerateImage()

    writer = itk.ImageFileWriter.IUC2.New()
    caster = itk.CastImageFilter.IF2IUC2.New()

    startPos = -10

    #drrLat._volume.RotateWXYZ(1,0,0,startPos)
    drrLat._volume.SetPosition(startPos,startPos,0)

    for i in range(11):
        for j in range(11):
            # Make some change to DRR here:
            #drrLat._volume.RotateWXYZ(1,0,0,1)
            drrLat._volume.SetPosition(startPos+i*2,startPos+j*2,0)
            drrLat._renWin.Render()

            start=time.clock()
            drrLat.GenerateImage()
            end1=time.clock()-start

            caster.SetInput(drrLat.GetImage('itk'))
            writer.SetInput(caster.GetOutput())
            saveName = outPath+baseName+'_x%sy%s.png' % (str(startPos+i*2),str(startPos+j*2))
            writer.SetFileName(saveName)
            writer.Write()

            print ("Writing Image: %s, Time(s): %.4f") % (saveName,end1)


if __name__ == "__main__":
    Test()